package com.iflytek.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.entity.Administrator;
import com.iflytek.mapper.AdministratorMapper;
import com.iflytek.query.AdministratorQuery;
import com.iflytek.service.AdministratorService;
import com.iflytek.utils.enums.ResultEnum;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Service
public class AdministratorServiceImpl extends ServiceImpl<AdministratorMapper, Administrator>
        implements AdministratorService {

    @Override
    public PageInfo<Administrator> getAllAdministratorList(AdministratorQuery query, Integer pageName, Integer pageSize) {
        if (query == null) {
            return null;
        }
        PageHelper.startPage(pageName, pageSize);
        List<Administrator> classrooms = this.getBaseMapper().selectAdministratorList(query);
        PageInfo<Administrator> pageInfo = new PageInfo<>(classrooms);
        return pageInfo;

    }

    @Override
    public Administrator getAdminPhone(String phone) {
        if (StringUtils.isEmpty(phone)) {
            return null;
        }
        return this.getBaseMapper().selectOne(new QueryWrapper<Administrator>().eq("phone", phone));
    }

    @Override
    public Administrator getAdminAccount(String account) {
        if (StringUtils.isEmpty(account)) {
            return null;
        }
        return this.getBaseMapper().selectOne(new QueryWrapper<Administrator>().eq("account", account));
    }

    @Override
    public Administrator getAdminEmail(String email) {
        if (StringUtils.isEmpty(email)) {
            return null;
        }
        return this.getBaseMapper().selectOne(new QueryWrapper<Administrator>().eq("email", email));
    }


    @Override
    public Administrator getByName(String name) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        return this.getBaseMapper().selectOne(new QueryWrapper<Administrator>().eq("name", name));
    }

    @Override
    public int delBatchByIds(List<String> ids) {
        if (ids == null) {
            return ResultEnum.FAILURE.getCode();
        }
        int del = this.getBaseMapper().deleteBatchIds(ids);
        return del >= 1 ? ResultEnum.SUCCESS.getCode() : ResultEnum.FAILURE.getCode();
    }

    @Override
    public int deleteById(Integer id) {
        if (id == null) {
            return ResultEnum.FAILURE.getCode();
        }
        //检查是否存在，存在就删除
        Administrator administrator = this.getBaseMapper().selectById(id);
        if (administrator == null) {
            return ResultEnum.FAILURE.getCode();
        }
        return this.getBaseMapper().deleteById(id);
    }

    @Override
    public int updateMsgById(Administrator administrator) {
        if (administrator == null) {
            return ResultEnum.FAILURE.getCode();
        }
        //查询信息
        Administrator oldAdministrator = this.getBaseMapper().selectById(administrator.getId());
        if (oldAdministrator.getId() == null) {
            return ResultEnum.FAILURE.getCode();
        }
        //管理员手机是否存在
        if (!oldAdministrator.getPhone().equals(administrator.getPhone())) {
            Administrator administrator1 = getByName(administrator.getPhone());
            if (administrator1 != null) {
                return ResultEnum.ADMIN_PHONE_FAILURE.getCode();//表示管理员手机存在
            }
        }

        //管理员账号是否存在
        if (!oldAdministrator.getAccount().equals(administrator.getAccount())) {
            Administrator administrator1 = getByName(administrator.getAccount());
            if (administrator1 != null) {
                return ResultEnum.ADMIN_ACCOUNT_FAILURE.getCode();//表示管理员账号存在
            }
        }
        //管理员邮箱是否存在
        if (!oldAdministrator.getEmail().equals(administrator.getEmail())) {
            Administrator adminEmail = getAdminEmail(administrator.getEmail());
            if (adminEmail != null) {
                return ResultEnum.ADMIN_EMAIL_FAILURE.getCode();//表示管理员邮箱存在
            }
        }

        return this.getBaseMapper().updateById(administrator);
    }

    @Override
    public int saveAdministrator(Administrator administrator) {
        if (administrator == null) {
            return ResultEnum.FAILURE.getCode();
        }
        //管理员账号重复
        Administrator adminAccount = getAdminAccount(administrator.getAccount());
        if (adminAccount != null) {
            return ResultEnum.ADMIN_ACCOUNT_FAILURE.getCode();//表示管理员账号存在
        }
        //管理员手机号重复
        Administrator adminPhone = getAdminPhone(administrator.getPhone());
        if (adminPhone != null) {
            return ResultEnum.ADMIN_PHONE_FAILURE.getCode();//表示管理员电话存在
        }
        //管理员邮箱重复
        Administrator adminEmail = getAdminEmail(administrator.getEmail());
        if (adminEmail != null) {
            return ResultEnum.ADMIN_EMAIL_FAILURE.getCode();//表示管理员邮箱存在
        }

        return this.getBaseMapper().insert(administrator);
    }
}




